package com.casicloud.manage.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.casicloud.manage.model.Menu;
import com.casicloud.manage.service.MenuService;
import com.common.sub.util.ControllerBase;
import com.common.sub.util.JsonUtils;
import com.common.sub.util.ZtreeModel;
@Controller
@RequestMapping(value = "menu")
public class MenuController extends ControllerBase {
	
	
	@Autowired
	private MenuService menuService;
	
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String goupdate(Menu menu,HttpServletRequest request,HttpServletResponse response){
		
		if(menu!=null&&menu.getId()!=null){
			
			menu  = menuService.findByOne(menu);
		}
		request.setAttribute("m", menu);
		Menu pm = new Menu();
		pm.setId(menu.getPid());
		pm = menuService.findByOne(pm);
		if(pm!=null){
			request.setAttribute("pm", pm);
		}
		
		
		return "menu/update";
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String doupdate(Menu menu,HttpServletRequest request,HttpServletResponse response){
		
		menu.setCreateTime(new Date());
		menuService.update(menu);
		
		return this.formSuccee();
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String goadd(Menu menu,HttpServletRequest request,HttpServletResponse response){
		Integer id = menu.getId();
		if(id!=null&&id!=0){
			Menu m  = menuService.findByOne(menu);
			request.setAttribute("m", m);
		}
		return "menu/add";
	}
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String doadd(Menu menu,HttpServletRequest request,HttpServletResponse response){
		
		if(menu.getPid()==null){
			menu.setPid(0);
		}
		if(menu.getIcon()==null||menu.getIcon().trim().length()<5){
			menu.setIcon("resource/images/menu/001.png");
		}
		
		menu.setCreateTime(new Date());
		
		menuService.save(menu);
		
		return this.formSuccee();
	}
	@RequestMapping(value = "delete")
	public String delete(Menu menu,HttpServletRequest request,HttpServletResponse response){
		
		if(menu!=null&&menu.getId()!=null){
			
			menuService.delete(menu);
		}
		
		return this.formSuccee();
	}
	@RequestMapping(value = "tree")
	public String tree(Menu menu,HttpServletRequest request,HttpServletResponse response){
		
		List<Menu> mlist = menuService.findByList(new Menu());
		
		List<ZtreeModel> zlist = new ArrayList<ZtreeModel>();
		
		
		for(int i = 0;i<mlist.size();i++){
			Menu m= mlist.get(i);
			ZtreeModel zm = new ZtreeModel();
			zm.setId(m.getId());
//			if(rlist.contains(m.getId())){
//				zm.setChecked(true);
//				zm.setOpen(true);
//			}
			zm.setName(m.getName());
			zm.setIcon(m.getIcon());
			zm.setpId(m.getPid());
			zlist.add(zm);
		}
		
		
		request.setAttribute("json", JsonUtils.ObjectToString(zlist));
		
		return "menu/tree";
		
		
	}
	
	

}
